Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create host and client functionality for context provider #3

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,3 @@ jobs:
GITHUB_TOKEN: ${{ secrets.D2L_RELEASE_TOKEN }}
MINOR_RELEASE_WITH_LMS: true
NPM: true
- name: Get new git HEAD
id: git
run: echo "head=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,104 @@ Install from CodeArtifact:
npm install @d2l/lms-context-provider
```

## Usage
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love any of this and I can't say I'm great at writing documentation. Very much open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good! One thing I try to personally do when writing docs is to avoid you/your/you're/etc. So like this:

If you do need to initialize a host, simply import and execute the initialize function.

Would become something like this:

To initialize a host, import and execute the initialize() function.


### 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.

```js
import { tryGet } from '@d2l/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.

```js
import { tryPerform } from '@d2l/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.

```js
import { initialize } from '@d2l/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.

```js
import { registerPlugin } from '@d2l/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.

```js
import { allowFrame } from '@d2l/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.
Expand Down
Loading