@modern-js/runtime-utils 2.49.4 → 2.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,31 +18,31 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var createRequestCtx_exports = {};
20
20
  __export(createRequestCtx_exports, {
21
- RequestContext: () => RequestContext,
22
21
  createRequestContext: () => createRequestContext
23
22
  });
24
23
  module.exports = __toCommonJS(createRequestCtx_exports);
25
- class RequestContext {
26
- get(loaderCtx) {
27
- const { symbol } = loaderCtx;
28
- if (this.store.get(symbol)) {
29
- return this.store.get(symbol);
24
+ function createRequestContext(context) {
25
+ const _context = context || /* @__PURE__ */ new Map();
26
+ function get(key) {
27
+ if (typeof key === "string") {
28
+ return _context.get(key);
29
+ } else {
30
+ return _context.get(key.symbol) || key.getDefaultValue();
30
31
  }
31
- return loaderCtx.getDefaultValue();
32
32
  }
33
- set(loaderCtx, value) {
34
- const { symbol } = loaderCtx;
35
- this.store.set(symbol, value);
36
- }
37
- constructor() {
38
- this.store = /* @__PURE__ */ new Map();
33
+ function set(key, value) {
34
+ if (typeof key === "string") {
35
+ _context.set(key, value);
36
+ } else {
37
+ _context.set(key.symbol, value);
38
+ }
39
39
  }
40
- }
41
- function createRequestContext() {
42
- return new RequestContext();
40
+ return {
41
+ set,
42
+ get
43
+ };
43
44
  }
44
45
  // Annotate the CommonJS export names for ESM import in node:
45
46
  0 && (module.exports = {
46
- RequestContext,
47
47
  createRequestContext
48
48
  });
@@ -1,28 +1,24 @@
1
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- var RequestContext = /* @__PURE__ */ function() {
3
- "use strict";
4
- function RequestContext2() {
5
- _class_call_check(this, RequestContext2);
6
- this.store = /* @__PURE__ */ new Map();
1
+ function createRequestContext(context) {
2
+ var _context = context || /* @__PURE__ */ new Map();
3
+ function get(key) {
4
+ if (typeof key === "string") {
5
+ return _context.get(key);
6
+ } else {
7
+ return _context.get(key.symbol) || key.getDefaultValue();
8
+ }
7
9
  }
8
- var _proto = RequestContext2.prototype;
9
- _proto.get = function get(loaderCtx) {
10
- var symbol = loaderCtx.symbol;
11
- if (this.store.get(symbol)) {
12
- return this.store.get(symbol);
10
+ function set(key, value) {
11
+ if (typeof key === "string") {
12
+ _context.set(key, value);
13
+ } else {
14
+ _context.set(key.symbol, value);
13
15
  }
14
- return loaderCtx.getDefaultValue();
15
- };
16
- _proto.set = function set(loaderCtx, value) {
17
- var symbol = loaderCtx.symbol;
18
- this.store.set(symbol, value);
16
+ }
17
+ return {
18
+ set,
19
+ get
19
20
  };
20
- return RequestContext2;
21
- }();
22
- function createRequestContext() {
23
- return new RequestContext();
24
21
  }
25
22
  export {
26
- RequestContext,
27
23
  createRequestContext
28
24
  };
@@ -1,23 +1,24 @@
1
- class RequestContext {
2
- get(loaderCtx) {
3
- const { symbol } = loaderCtx;
4
- if (this.store.get(symbol)) {
5
- return this.store.get(symbol);
1
+ function createRequestContext(context) {
2
+ const _context = context || /* @__PURE__ */ new Map();
3
+ function get(key) {
4
+ if (typeof key === "string") {
5
+ return _context.get(key);
6
+ } else {
7
+ return _context.get(key.symbol) || key.getDefaultValue();
6
8
  }
7
- return loaderCtx.getDefaultValue();
8
9
  }
9
- set(loaderCtx, value) {
10
- const { symbol } = loaderCtx;
11
- this.store.set(symbol, value);
12
- }
13
- constructor() {
14
- this.store = /* @__PURE__ */ new Map();
10
+ function set(key, value) {
11
+ if (typeof key === "string") {
12
+ _context.set(key, value);
13
+ } else {
14
+ _context.set(key.symbol, value);
15
+ }
15
16
  }
16
- }
17
- function createRequestContext() {
18
- return new RequestContext();
17
+ return {
18
+ set,
19
+ get
20
+ };
19
21
  }
20
22
  export {
21
- RequestContext,
22
23
  createRequestContext
23
24
  };
@@ -1,7 +1,16 @@
1
+ import { RequestPayload } from '@modern-js/types';
1
2
  import { LoaderContext } from './createLoaderCtx';
2
- export declare class RequestContext {
3
- private store;
4
- get<T>(loaderCtx: LoaderContext<T>): T;
5
- set<T = unknown>(loaderCtx: LoaderContext, value: T): void;
3
+ interface Get<P extends Record<string, unknown>> {
4
+ <Key extends keyof P>(key: Key): P[Key];
5
+ <T>(key: LoaderContext<T>): T;
6
6
  }
7
- export declare function createRequestContext(): RequestContext;
7
+ interface Set<P extends Record<string, unknown>> {
8
+ <Key extends keyof P>(key: Key, value: P[Key]): void;
9
+ <T>(key: LoaderContext<T>, value: T): void;
10
+ }
11
+ export type RequestContext<P extends Record<string, unknown> = {}> = {
12
+ get: Get<P & RequestPayload>;
13
+ set: Set<P & RequestPayload>;
14
+ };
15
+ export declare function createRequestContext<P extends Record<string, unknown>>(context?: Map<string | symbol, unknown>): RequestContext<P>;
16
+ export {};
@@ -1,4 +1,16 @@
1
1
  import type { Reporter } from '@modern-js/types';
2
2
  export { createRequestContext, type RequestContext } from './createRequestCtx';
3
3
  export type { LoaderContext } from './createLoaderCtx';
4
+ /**
5
+ * @deprecated
6
+ * Use `context.get('reporter')` instead of `context.get(reporterCtx)`. The `reporterCtx` will be removed in next major version.
7
+ *
8
+ * @example
9
+ *
10
+ * const loader = ({ context }: LoaderFunctionArgs) => {
11
+ * const reporter = context?.get('reporter')
12
+ * // doSomething
13
+ * }
14
+ *
15
+ */
4
16
  export declare const reporterCtx: import("./createLoaderCtx").LoaderContext<Reporter>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.49.4",
18
+ "version": "2.50.0",
19
19
  "_comment": "Provide ESM and CJS exports, ESM is used by runtime package, for treeshaking",
20
20
  "exports": {
21
21
  "./router": {
@@ -114,7 +114,7 @@
114
114
  "react-router-dom": "6.22.0",
115
115
  "@remix-run/router": "1.15.0",
116
116
  "@swc/helpers": "0.5.3",
117
- "@modern-js/utils": "2.49.4"
117
+ "@modern-js/utils": "2.50.0"
118
118
  },
119
119
  "peerDependencies": {
120
120
  "react": ">=17.0.0",
@@ -137,9 +137,9 @@
137
137
  "jest": "^29",
138
138
  "typescript": "^5",
139
139
  "@types/serialize-javascript": "^5.0.1",
140
- "@modern-js/types": "2.49.4",
141
- "@scripts/jest-config": "2.49.4",
142
- "@scripts/build": "2.49.4"
140
+ "@modern-js/types": "2.50.0",
141
+ "@scripts/build": "2.50.0",
142
+ "@scripts/jest-config": "2.50.0"
143
143
  },
144
144
  "sideEffects": false,
145
145
  "scripts": {