How to use the mobx-keystone.applySerializedActionAndTrackNewModelIds function in mobx-keystone

To help you get started, we’ve selected a few mobx-keystone 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 xaviergonz / mobx-keystone / packages / site / src / examples / clientServer / server.ts View on Github external
setTimeout(() => {
      // apply the action over the server root store
      // sometimes applying actions might fail (for example on invalid operations
      // such as when one client asks to delete a model from an array and other asks to mutate it)
      // so we try / catch it
      let serializedActionCallToReplicate: SerializedActionCallWithModelIdOverrides | undefined
      try {
        // we use this to apply the action on the server side and keep track of new model IDs being
        // generated, so the clients will have the chance to keep those in sync
        const applyActionResult = applySerializedActionAndTrackNewModelIds(
          this.serverRootStore,
          actionCall
        )
        serializedActionCallToReplicate = applyActionResult.serializedActionCall
      } catch (err) {
        console.error("error applying action to server:", err)
      }

      if (serializedActionCallToReplicate) {
        setTimeout(() => {
          // and distribute message, which includes new model IDs to keep them in sync
          this.msgListeners.forEach(listener => listener(serializedActionCallToReplicate!))
        }, 500)
      }
    }, 500)
  }