@brightspace-ui/lms-context-provider

中文文档
1.35.0发布于 2026/10/19Apache-2.0ES Module零依赖近 30 天下载 2935

Provides LMS context and environment settings to UI components, applications and libraries

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Using a Client
  4. Configuring a Host
  5. Developing, Testing and Contributing
  6. Linting
  7. Testing
  8. Versioning and Releasing

@brightspace-ui/lms-context-provider

Provides LMS context and environment settings to UI components, applications and libraries.

Installation

Install from NPM:

npm install @brightspace-ui/lms-context-provider

Usage

Using a Client

Requesting Data

To request data, import tryGet from the client and invoke it directly. The first argument should be a context type corresponding to a registered host plugin, while the second argument is an optional set of options to pass through to the host plugin. The third argument is an optional callback to allow a consumer to subscribe to future changes to the data they're requesting.

import { tryGet } from '@brightspace-ui/lms-context-provider/client.js';

const val = await tryGet(
  'my-context-type',
  { someProp: someVal },
  (changedValues) => {
    // This callback should accept a single argument:
    // an object containing any relevant information from the host plugin
    if (changedValues.someChangedProp === 'someVal') {
        doSomeWork(changedValues.someChangedProp);
    }
  }
);
doSomeWork(val);

If no host plugin is registered to handle a request, or if the data being requested isn't available, the host will return undefined. The host plugin may also need to rely on asynchronous methods to return data, so client code should be resilient to receiving a promise that doesn't resolve or takes some time to resolve.

If no host has been initialized, tryGet will reject with an error.

Performing an Action

To initiate an action on the host but doesn't require return data, import tryPerform from the client and invoke it directly. The first argument should be a context type corresponding to a registered host plugin, while the second argument is an optional set of options to pass through to the host plugin. It is not possible to subscribe to change events using this function.

import { tryPerform } from '@brightspace-ui/lms-context-provider/client.js;'

await tryPerform('my-context-type', { someProp: someVal });

If no host plugin is registered to handle a request, or if the data being requested isn't available, this promise will immediately resolve and nothing will happen. As with the tryGet function, the host plugin may need to perform asynchronous actions to fulfill a request, so this promise may also never resolve, or may take some time to resolve.

If no host has been initialized, tryPerform will reject with an error.

Configuring a Host

Initializing

Initializing a host should rarely be necessary. Within a Brightspace instance, this will generally be handled by BSI via our MVC and legacy frameworks. To initialize a host, import and execute the initialize function.

import { initialize } from '@brightspace-ui/lms-context-provider/host.js';

initialize();

Registering Plugins

To register a host plugin, import and execute the registerPlugin function on a page where a host has already been initialized. The provided context type should be unique per page. If a plugin needs to return data to a client, it should provide a tryGetCallback as the second argument. If clients can be notified when the data changes, then it should provide a subscribeCallback as the third argument.

import { registerPlugin } from '@brightspace-ui/lms-context/provider/host.js';

function tryGetCallback(options) {
    // This can be asynchronous.
    const returnVal = doSomeWork(options);
    return returnVal;
}

function subscribeCallback(onChange, options) {
    // this can be asynchronous.
    const returnVal = doSomeWork(options);

    // Options are defined by the host, not the plugin. sendImmediate indicates the change handler should be invoked immediately.
    if (options.sendImmediate) {
        // Expects an object as its only argument.
        onChange({ val: returnVal });
    }

    // onChange event should be subscribed to future changes.
    registerOnChangeEvent(onChange);
}

registerPlugin('my-context-type', tryGetCallback, subscribeCallback);

Framed Clients

When working with a client inside an iframe, the host page needs to explicitly allow that iframe. To do this, import and execute allowFrame from the host page (the host must already be initialized.). The first argument must be the iframe element itself. The second argument should be the expected origin. Requests from clients within iframes that have not explicitly been allowed or that come from a different origin will be rejected.

import { allowFrame } from '@brightspace-ui/lms-context-provider/host.js';

const myFrame = document.createElement('iframe');
document.body.append(myFrame);

allowFrame(myFrame, window.location.origin);

Developing, Testing and Contributing

After cloning the repo, run npm install to install dependencies.

Linting

# eslint
npm run lint

Testing

# run tests
npm test

Versioning and Releasing

This repo is configured to use semantic-release. Commits prefixed with fix: and feat: will trigger patch and minor releases when merged to main.

To learn how to create major releases and release from maintenance branches, refer to the semantic-release GitHub Action documentation.

版本与发布记录

@brightspace-ui/lms-context-provider 共发布 34 个版本,首次发布于 2024/05/14;过去 12 个月发布了 12 个版本。

版本发布日期距今
1.35.0latest2026/10/1920 天前
1.34.02026/09/141 个月前
1.33.02026/08/172 个月前
1.32.02026/07/203 个月前
1.31.02026/06/154 个月前
1.30.02026/05/185 个月前

关于 @brightspace-ui/lms-context-provider 的常见问题

@brightspace-ui/lms-context-provider 是什么?

Provides LMS context and environment settings to UI components, applications and libraries

如何安装 @brightspace-ui/lms-context-provider?

在项目目录运行 npm install @brightspace-ui/lms-context-provider,或使用 pnpm add / yarn add / bun add。

@brightspace-ui/lms-context-provider 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/brightspace-ui__lms-context-provider 是否存在。

@brightspace-ui/lms-context-provider 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@brightspace-ui/lms-context-provider 可以商用吗?

@brightspace-ui/lms-context-provider 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@brightspace-ui/lms-context-provider 的下载量有多少?

根据 npm 官方下载统计,@brightspace-ui/lms-context-provider 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,935 次,日均约 98 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brightspace-ui/lms-context-provider 还在维护吗?

最新版本 1.35.0 发布于 2026/10/19(20 天前);至今共发布 34 个版本,其中过去 12 个月发布了 12 个;首次发布于 2024/05/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。