Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/starhoshi/mission-completed
Stop processing even if Cloud Functions fires multiple times.
https://github.com/starhoshi/mission-completed
cloudfunctions firebase firestore javascript npm typescript
Last synced: about 3 hours ago
JSON representation
Stop processing even if Cloud Functions fires multiple times.
- Host: GitHub
- URL: https://github.com/starhoshi/mission-completed
- Owner: starhoshi
- License: mit
- Created: 2018-02-11T06:33:00.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-04T07:37:32.000Z (over 6 years ago)
- Last Synced: 2024-11-07T10:41:21.851Z (12 days ago)
- Topics: cloudfunctions, firebase, firestore, javascript, npm, typescript
- Language: TypeScript
- Homepage:
- Size: 354 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mission-completed
[![npm version](https://badge.fury.io/js/mission-completed.svg)](https://badge.fury.io/js/mission-completed)
[![Build Status](https://travis-ci.org/starhoshi/mission-completed.svg?branch=master)](https://travis-ci.org/starhoshi/mission-completed)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bb251ccd81c148cda6cf7322da394e3a)](https://www.codacy.com/app/kensuke1751/mission-completed?utm_source=github.com&utm_medium=referral&utm_content=starhoshi/mission-completed&utm_campaign=Badge_Grade)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)> Note: Trigger events are delivered at least once, which means that rarely, spurious duplicates may occur.
> https://cloud.google.com/functions/docs/concepts/events-triggers#triggersCloud Functions rarely fire multiple times.
If __multiple payment occurs__, it is a big problem!!mission-completed uses __transactions__ to prevent multiple trigger events.
If you try to set the completed flag to true many times, CompletedError will be returned.
```ts
await Mission.markCompleted(ref, id) // first: success
await Mission.markCompleted(ref, id) // second: throw CompletedError
```Results are saved like this.
## Install
```
yarn install mission-completed
```## Usage
This sample is written in TypeScript.
### 1. Initialize
Initialize event-response in your index.ts.
```ts
import * as Mission from 'mission-completed'
import * as functions from 'firebase-functions'Mission.initialize(functions.config().firebase)
```### 2. mark completed in Cloud Functions
If mission has already been completed, throw CompletedError in `Mission.markCompleted(event.data.ref, 'updateUser')`.
```ts
exports.updateUser = functions.firestore.document('users/{userId}')
.onCreate(async event => {
try {
await Mission.markCompleted(event.data.ref, 'updateUser')
} catch (error) {
if (error.constructor === Mission.CompletedError) {
console.error(error, 'Mission has already been completed.')
return Promise.reject(error)
}
}await event.data.ref.update({updated: true})
return undefined
})
```This will continue the initial process, but the simultaneous firing process will stop with CompletedError.
## License
MIT