电子在服务器端环境中的“实时禁用” AppSync“实时禁用”

发布于 2025-01-22 19:18:44 字数 243 浏览 1 评论 0原文

我正在我的电子应用中使用Amplify AppSync,除了实时事件外,所有工作都可以使用,这意味着当数据在云中更改时,我的应用不会发出信号。

我知道这是因为它在节点中运行,这就是Amplify正在寻找的(js.browserornode),但是我不知道如何修复它。有人可以建议我如何获得实时更新吗?当我查询时,除非我dataStore.stop()和datastore.start(),否则它只是获取本地数据,但肯定有更好的方法?

先感谢您!

I'm using amplify appsync in my electron app and all works except for the realtime events which means my app doesn't get signalled when the data changes in the cloud.

I know this is because it's running in Node which is what amplify is looking for (JS.browserOrNode) however I don't know how to fix it. Can someone advise me how I can get the realtime updates? When I query it just gets the local data unless I Datastore.stop() and Datastore.start() but surely there's a better way?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇俏 2025-01-29 19:18:44

多亏了Discord上的Amplify团队,在Amplify-js中,浏览器与节点检查的逻辑是:

const browserOrNode = () => {
    const isBrowser =
        typeof window !== 'undefined' && typeof window.document !== 'undefined';
    const isNode =
        typeof process !== 'undefined' &&
        process.versions != null &&
        process.versions.node != null;

    return {
        isBrowser,
        isNode,
    };
};

存储逻辑确定它是否会吐出警告或启动订阅

if (isNode) {
    logger.warn(
        'Realtime disabled when in a server-side environment'
    );
} else {
    //#region GraphQL Subscriptions
    [
        // const ctlObservable: Observable<CONTROL_MSG>
        ctlSubsObservable,
        // const dataObservable: Observable<[TransformerMutationType, SchemaModel, Readonly<{
        // id: string;
        // } & Record<string, any>>]>
        dataSubsObservable,
    ] = this.subscriptionsProcessor.start();
}

数据 .js环境,这就是为什么数据存储在实时禁用。

您可以通过强制介绍为false,然后实时与Electron一起使用:)

Thanks to the amplify team on discord for this one, in amplify-js the logic for the browser vs node check is:

const browserOrNode = () => {
    const isBrowser =
        typeof window !== 'undefined' && typeof window.document !== 'undefined';
    const isNode =
        typeof process !== 'undefined' &&
        process.versions != null &&
        process.versions.node != null;

    return {
        isBrowser,
        isNode,
    };
};

The Datastore logic determines whether it's going to spit that warning or start the subscriptions:

if (isNode) {
    logger.warn(
        'Realtime disabled when in a server-side environment'
    );
} else {
    //#region GraphQL Subscriptions
    [
        // const ctlObservable: Observable<CONTROL_MSG>
        ctlSubsObservable,
        // const dataObservable: Observable<[TransformerMutationType, SchemaModel, Readonly<{
        // id: string;
        // } & Record<string, any>>]>
        dataSubsObservable,
    ] = this.subscriptionsProcessor.start();
}

But because Electron runs the app in a Node.js environment it's why DataStore is disabling realtime.

You can stop this by forcing isNode to be false and then realtime works with electron :)

暗恋未遂 2025-01-29 19:18:44

感谢 @s.mac,

更澄清的是S.Mac回答我如何修复我的。

"node version": "v19.2.0",
"npm version": "9.2.0",
"aws-amplify": "^5.0.4",
"patch-package": "^6.5.0"
  1. 打开软件包。

  2. install patch-package npm i Patch-packagenpm i- -save-dev补丁包
    如果您喜欢

  3. 打开“ node_module/@aws-amplify/core/core/lib-ems/js.js”,然后向下滚动至

export var browserOrNode = function(){}

中替换所有语句

return {
  isBrowser: true,
  isNode: false,
};
  1. 并用open open open``node_modules /@ aws-amplify/core/core/src/js.ts'' 。
    导出const browserornode =()=&gt; {}函数如果您使用的是TS,然后将语句更改为:
const isBrowser = true;
const isNode = false;

return {
  isBrowser,
  isNode,
};
  1. 现在,最终使用:npx patch-package aws-amplify确保您已连接到Internet ,我不知道为什么。

希望这对某人和我有帮助。

Thank @S.Mac,

A more clarification on S.Mac answer how I fixed mine.

"node version": "v19.2.0",
"npm version": "9.2.0",
"aws-amplify": "^5.0.4",
"patch-package": "^6.5.0"
  1. Open package.json and add "postinstall": "patch-package" to script

  2. Install patch-package npm i patch-package or npm i --save-dev patch-package
    if you prefer

  3. Open "node_module/@aws-amplify/core/lib-ems/JS.js" and scroll down to

export var browserOrNode = function(){}

and replace all the statement in with

return {
  isBrowser: true,
  isNode: false,
};
  1. Open "node_modules/@aws-amplify/core/src/JS.ts" and find
    export const browserOrNode = () => {} function if you're using TS and change the statement in to:
const isBrowser = true;
const isNode = false;

return {
  isBrowser,
  isNode,
};
  1. Now finally, patch the package back using : npx patch-package aws-amplify ensure you're connected to internet, i don't know why.

Hope this will be helpfull to someone and me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文