How to use the most.create function in most

To help you get started, we’ve selected a few most examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DerekCuevas / friend-list / motorcyclejs-solution / src / api / index.js View on Github external
export default function search(query = ``) {
  const results = friends.filter(friend => {
    const keys = Object.keys(friend)
    // faster search
    for (let i = 0; i < keys.length; ++i) {
      const val = friend[keys[i]].toString().toLowerCase()
      if (val.includes(query.toLowerCase())) {
        return true
      }
    }
    return false
  })

  // use an stream for our search API so that it's actually cancellable when we dispose of our subscription
  return create((next) => {
    const timeout = setTimeout(() => {
      console.log(`RESOLVING search ${timeout}`)
      next(results)
    }, Math.ceil(100 + Math.random() * 500)) // make delay longer to make cancellation and loading screen obvious

    next('loading') // send 'loading' state
    console.log(`STARTING search ${timeout}`)

    return () => {
      console.log(`DISPOSING search ${timeout}`)
      clearTimeout(timeout)
    }
  }).startWith(friends)
}
github izaakschroeder / afflux / lib / combinators / pipeline.js View on Github external
export default function createRenderStream(callback, watch) {
	const parent = create((add, end, err) => {
		let done = false;

		function chunk() {
			// Create two streams - one with all the events that have happened
			// since the last time we ran callback, and another which waits for
			// the results of any promises that happened to be emitted. This
			// allows waiting for the resolution of promises before proceeding
			// to the next invocation.
			const events = await(until(parent, map(resolve, watch)));

			// So if anything has happened on the actions then keep processing,
			// otherwise we're safe to end the stream. Just pass up any errors
			// that occur. If there are no more listeners on the stream then
			// also stop processing.
			empty(events)
				.then(none => none || done ? defer(end) : chunk())
github izaakschroeder / afflux / lib / action.js View on Github external
export default function wrap(handler, name, descriptor) {
	if (isFunction(handler)) {
		// This is kind of an unusual pattern, but necessary for `most`. When
		// someone is observing the action, then `add` will be a valid function
		// that writes to the action stream; when no one is observing then `add`
		// will be null.
		let add = null, stream = null;
		const results = create((a) => {
			add = a;
			stream.active = true;
			return () => {
				add = null;
				stream.active = false;
			};
		});

		// Create a function that acts like a `most` stream but is callable.
		stream = assign(function action() {
			const result = handler.apply(null, arguments);
			// The result is only added to the event stream if someone is
			// actually listening for events.
			if (add) {
				add(result);
			}
github interlockjs / interlock / lib / index.es6 View on Github external
Interlock.prototype.watch = function (save=false) {
  const self = this;
  let lastCompilation = null;
  const absPathToModuleHash = Object.create(null);

  const watcher = watch([], {});

  return most.create(add => {
    function onCompileComplete (compilation) {
      lastCompilation = compilation;
      for (let [, bundleObj] of entries(compilation.bundles)) {
        for (let module of bundleObj.modules || []) {
          watcher.add(module.path);
          absPathToModuleHash[module.path] = module.hash;
        }
      }
      if (save) { self._saveBundles(compilation); }
      // Emit compilation.
      add({ compilation });
    }

    watcher.on("change", changedFilePath => {
      for (let modulePath of Object.keys(absPathToModuleHash)) { watcher.unwatch(modulePath); }
github izaakschroeder / afflux / test / spec / render.spec.js View on Github external
function publisher() {
	const s = create((add) => s.add = add, () => s.add = null);
	return s;
}
github AlexGalays / kaiju / src / globalStream.js View on Github external
export function GlobalStream(initialState, registerHandlers, log) {
  if (log === undefined) log = defaultLog.stream;

  const handlers = {};
  let dispatching = false;

  const on = (msg, fn) => { handlers[msg._id] = fn }
  registerHandlers(on);

  if (log)
    console.log('%cGlobal stream - initial state:', 'color: green', initialState);

  const stream = most.create((add, end, error) => {
    stream.send = function(message) {
      const { _id, _name, payload } = message;

      if (log)
        console.log('%c' + _name, 'color: #C93CBD', 'sent globally with payload ', payload);

      if (dispatching) throw new Error(
        'Cannot dispatch a Msg in the middle of another msg\'s dispatch');

      const handler = handlers[_id];
      if (!handler) {
        throw new Error('globalStream.send: Unknown message: ', _name);
        return;
      }

      dispatching = true;
github funkia / hareactive / benchmark / scan.ts View on Github external
.add("most", function(defered: any): void {
    most.create(function(add: ((n: number) => void)): any {
      let i = 0;
      let l = testData.length;
      for (; i < l; i++) {
        add(testData[i]);
      }
    }).scan(sum, 0)
      .observe(function(e: number): void {
        if (e === result) {
          defered.resolve();
        }
    });
  }, {defer: true})
github mathieuancelin / js-repaint-perfs / motorcycle / src / app.js View on Github external
  databases: function() {return most.create(load)}
})
github TylorS / stream-conversions / src / to.js View on Github external
const toMost = (stream, streamInterface) =>
  Most.create((add, end, error) => {
    const observer = {
      onNext: add,
      onCompleted: end,
      onError: error,
    }
    streamInterface(stream, observer)
  })